package com.example.knife4jdemo.config;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.text.SimpleDateFormat;
import java.util.Date;

@Getter
@Setter
@ApiModel("返回实体类")
public class ResponseDataDiy<T> {
    /**
     * 0:表示正常返回
     * -1:异常，异常信息从msg中获取
     */
//    @ApiModelProperty(value = "状态码", required = true, example = "200") //value也可以
    @ApiModelProperty(required = true, notes = "响应码", example = "0")
    private int code = 200;
//    @ApiModelProperty(value = "返回时间", example = "2020-02-29 12:12:12")
//    private String currentTime;
    @ApiModelProperty(required = true, value = "状态描述", example = "success")
    private String msg = "success";
//    @ApiModelProperty(value = "接口版本", example = "1.0")
//    private String version = "1.0";

    @ApiModelProperty(required = true, value = "数据", example = "200")
    public T data = null;

    public ResponseDataDiy() {
        Date timeInfo = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(timeInfo);
    }
    public ResponseDataDiy(int code, String msg){
        this.code = code;
        this.msg = msg;
    }
    public ResponseDataDiy(int code, String msg, T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

//    public static ResponseData ok(T data) {
//        this.data = data;
//    }


    /**
     * 返回错误信息
     *
     * @param code    错误码
     * @param message 错误信息
     * @return RestResponse
     */
    public static <T> ResponseDataDiy<T> error(Integer code, String message) {
        ResponseDataDiy<T> response = new ResponseDataDiy<>();
        response.setCode(code);
        response.setMsg(message);
        return response;
    }

    /**
     * 返回自定义信息、对象
     *
     * @param code    结果码
     * @param message 信息
     * @param obj     对象
     * @return RestResponse
     */
    public static <T> ResponseDataDiy<T> success(Integer code, String message, T obj) {
        ResponseDataDiy<T> response = new ResponseDataDiy<>();
        response.setCode(code);
        response.setMsg(message);
        response.setData(obj);
        return response;
    }

    public static <T> ResponseDataDiy<T> success(T obj) {
        ResponseDataDiy<T> response = new ResponseDataDiy<>();
        response.setCode(200);
        response.setMsg("成功");
        response.setData(obj);
        return response;
    }



}
